package com.forum.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.forum.article.entity.ForumArticleType;
import com.forum.article.mapper.ForumArticleTypeMapper;
import com.forum.article.service.ForumArticleTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.forum.article.vo.Result;
import com.forum.article.vo.TypeVO;
import com.forum.common.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@Service
public class ForumArticleTypeServiceImpl extends ServiceImpl<ForumArticleTypeMapper, ForumArticleType> implements ForumArticleTypeService {

    @Autowired
    ForumArticleTypeMapper forumArticleTypeMapper;


    @Override
    public TypeVO findTypeById(Long articleTypeId) {
        ForumArticleType forumArticleType = forumArticleTypeMapper.selectById(articleTypeId);
        TypeVO typeVO = new TypeVO();
        BeanUtils.copyProperties(forumArticleType,typeVO);
        typeVO.setId(forumArticleType.getId());
        //页面交互
        return typeVO;
    }

    @Override
    public R findAll() {
        List<ForumArticleType> forumArticleTypes = forumArticleTypeMapper.selectList(null);
//        页面交互
        return R.ok().put("success" , true).put("data" , forumArticleTypes);
    }

    @Override
    public Result findAllDetail() {
        LambdaQueryWrapper<ForumArticleType> queryWrapper = new LambdaQueryWrapper<>();
        List<ForumArticleType> forumArticleTypes = forumArticleTypeMapper.selectList(queryWrapper);

        return Result.success(copyList(forumArticleTypes));
    }

    @Override
    public Result typeDetailById(Long id) {
        ForumArticleType forumArticleType = forumArticleTypeMapper.selectById(id);
        return Result.success(copy(forumArticleType));
    }



    public TypeVO copy(ForumArticleType forumArticleType){
        TypeVO typeVO = new TypeVO();
        BeanUtils.copyProperties(forumArticleType,typeVO);
        typeVO.setId(forumArticleType.getId());
        return typeVO;
    }

    public List<TypeVO> copyList(List<ForumArticleType> forumArticleTypeList){
        List<TypeVO> typeVOList = new ArrayList<>();
        for (ForumArticleType forumArticleType:forumArticleTypeList){
            typeVOList.add(copy(forumArticleType));
        }
        return typeVOList;
    }
}
